﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_Staff : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string staff_id;

        public frm_Edit_Staff()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.staff_id = null;

            ComboboxItem item = new ComboboxItem();
            item.Text = "Male";
            item.Value = "1";
            this.sexValue.Properties.Items.Add(item);

            ComboboxItem item2 = new ComboboxItem();
            item2.Text = "Female";
            item2.Value = "2";
            this.sexValue.Properties.Items.Add(item2);
        }

        ////////////////////////////////////
        /// Load data to form
        ///////////////////////////////////        
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(staff_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_Staff] WHERE StaffID='" + staff_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.staffID.Text = dataReader["StaffID"].ToString();
                        this.staffName.Text = dataReader["StaffName"].ToString();
                        
                        if (dataReader["Birthday"] != DBNull.Value)
                        {
                            this.birthDay.EditValue = DateTime.Parse(dataReader["Birthday"].ToString()).Date;
                        } 
                        if (dataReader["HireDate"] != DBNull.Value)
                        {
                            this.hireDate.EditValue = DateTime.Parse(dataReader["HireDate"].ToString()).Date;
                        }
                        if (dataReader["StopDate"] != DBNull.Value)
                        {
                            this.stopDate.EditValue = DateTime.Parse(dataReader["StopDate"].ToString()).Date;
                        } 
                        this.secID.Text = dataReader["SecID"].ToString();
                        this.secName.Text = dataReader["SecName"].ToString();
                        this.groupID.Text = dataReader["GroupID"].ToString();
                        this.personalInf.Text = dataReader["PersonalInf"].ToString();
                        if (dataReader["Sex"] != DBNull.Value)
                        {
                            if ((bool)dataReader["Sex"])
                            {
                                this.sexValue.SelectedIndex = 0;
                            }
                            else
                            {
                                this.sexValue.SelectedIndex = 1;
                            }
                        }
                        else
                        {
                            this.sexValue.SelectedIndex = 1;
                        }
                        this.departmentValue.Text = dataReader["DepartmentID"].ToString();
                        this.educationValue.Text = dataReader["Education"].ToString();
                        this.addressValue.Text = dataReader["Address"].ToString();
                        this.phoneNumber.Text = dataReader["PhoneNumber"].ToString();
                        
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                    //MessageBox.Show(ex.Message);
                }
            }

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string staff_name = null;
            string sec_id = null;
            string sec_name = null;
            DateTime birthday = new DateTime();
            DateTime hiredate = new DateTime();
            DateTime stopdate = new DateTime();
            int sex = 0;
            string groupid = null;
            string personalinf = null;
            string department = null;
            string education = null;
            string address = null;
            string phonenumber = null;

            if (string.IsNullOrEmpty(this.staffName.Text))
            {
                error = "Please enter Staff Name";
            }
            else
            {
                staff_name = this.staffName.Text;
            }
            if (string.IsNullOrEmpty(error))
            {
                sec_id = this.secID.Text;
                sec_name = this.secName.Text;                
                groupid = this.groupID.Text;
                personalinf = this.personalInf.Text;
                if (this.sexValue.SelectedIndex == 0)
                {
                    sex = 1;
                }
                else
                {
                    sex = 0;
                }
                department = this.departmentValue.Text;
                education = this.educationValue.Text;
                address = this.addressValue.Text;
                phonenumber = this.phoneNumber.Text;
                            
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_Staff] SET StaffName = N'" + staff_name + "', [Birthday] = @birthday,"
                        + " [HireDate] = @hiredate, [StopDate] = @stopdate, SecID = '" + sec_id + "', SecName='" + sec_name + "',"
                        + " [GroupID] = '" + groupid + "', [PersonalInf] = '" + personalinf + "', [Sex] = " + sex + ","
                        + " [DepartmentID] = '" + department + "', [Education] = '" + education + "',"
                        + " [PhoneNumber] = '" + phonenumber + "', [Address] = '" + address + "'"
                        + " WHERE StaffID='" + staff_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    if (this.birthDay.EditValue != null)
                    {
                        birthday = DateTime.Parse(this.birthDay.EditValue.ToString());
                        cmd_sql.Parameters.Add("@birthday", SqlDbType.DateTime).Value = birthday.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@birthday", SqlDbType.DateTime).Value = DBNull.Value;
                    }
                    if (this.hireDate.EditValue != null)
                    {
                        hiredate = DateTime.Parse(this.hireDate.EditValue.ToString());
                        cmd_sql.Parameters.Add("@hiredate", SqlDbType.DateTime).Value = hiredate.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@hiredate", SqlDbType.DateTime).Value = DBNull.Value;
                    }
                    if (this.stopDate.EditValue != null)
                    {
                        stopdate = DateTime.Parse(this.stopDate.EditValue.ToString());
                        cmd_sql.Parameters.Add("@stopdate", SqlDbType.DateTime).Value = stopdate.Date;
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@stopdate", SqlDbType.DateTime).Value = DBNull.Value;
                    }
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Staff updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Staff. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
